package com.sys.orders.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.pojo.JSONEnum;
import com.sys.common.service.BaseServiceImpl;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.orders.dao.ITOrdersDao;
import com.sys.orders.dao.ITOrdersGoodsDao;
import com.sys.orders.dao.impl.TOrdersDaoImpl;
import com.sys.orders.dao.impl.TOrdersGoodsDaoImpl;
import com.sys.orders.pojo.TOrders;
import com.sys.orders.pojo.TOrdersGoods;
import com.sys.orders.service.IOrdersDbService;
import com.sys.users.pojo.TCart;
import com.sys.users.pojo.TCartEnum;
import com.sys.users.pojo.TUsers;
import com.sys.users.service.IUsersDbService;
import com.sys.wares.pojo.AWares;
import com.sys.wares.service.IGoodsDbService;

/**
 * Service实现类(订单代码)
 * 
 * @author 杨志超
 */
public class OrdersDbServiceImpl extends BaseServiceImpl implements IOrdersDbService
{
	private ITOrdersDao ordersDao = new TOrdersDaoImpl();
	private ITOrdersGoodsDao ordersGoodsDao = new TOrdersGoodsDaoImpl();
	
	private IUsersDbService usersDbService;
	private IGoodsDbService goodsDbService;
	
	@Override
	public ApiResponse<Object> saveOneOrdersService(TOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 判断折扣 */
		/* 判断是否是会员 */
		condMap.put("id", orders.getUsersId());
		ApiResponse<TUsers> usersApiResponse = this.usersDbService.findOneUsersService(condMap);
		TUsers users = usersApiResponse.getDataOneJava() ;
		double discount = users.getDiscount() ; 
		orders.setDiscount(discount);
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		int res = this.ordersDao.save(orders);
		if (res > 0)
		{
			double totalPrice = 0 ; 
			/* 存储订单明细 */
			for (Iterator iterator = orders.getCartList().iterator(); iterator.hasNext();)
			{
				TCart cart = (TCart) iterator.next();
				AWares goods = cart.getGoods() ; 
				
				if(goods.getStockNum() < cart.getNum())
				{
					ConstatFinalUtil.SYS_LOGGER.info("==库存不够:{}==",goods.getId());
					continue ; 
				}
				
				TOrdersGoods ordersGoods = new TOrdersGoods();
				ordersGoods.setOrdersId(orders.getId());
				ordersGoods.setGoodsId(cart.getGoodsId());
				
				ordersGoods.setName(goods.getName());
				ordersGoods.setTitle(goods.getTitle());
				ordersGoods.setPrice(goods.getPrice());
				ordersGoods.setNum(cart.getNum());
				ordersGoods.setTotalPrice(ordersGoods.getPrice() * ordersGoods.getNum());
				
				/* 总价格相加 */
				totalPrice = totalPrice + ordersGoods.getTotalPrice() ; 
				
				ordersGoods.setStatus(orders.getStatus());
				ordersGoods.setPubTime(new Date());
				/* 订单商品 */
				this.saveOneOrdersGoodsService(ordersGoods);
				
				cart.setNum(0);
				cart.setStatus(TCartEnum.STATUS_DISABLED.getStatus());
				cart.setUpdateTime(new Date());
				
				/* 清空购物车 */
				this.usersDbService.updateOneCartService(cart);
				
				/* 更新商品的销量 */
				goods.setSaleNum(goods.getSaleNum() + ordersGoods.getNum());
				goods.setStockNum(goods.getStockNum() - ordersGoods.getNum());
				goods.setUpdateTime(new Date());
				this.goodsDbService.updateOneGoodsService(goods);
			}
			
			/* 设置总价格 */
			orders.setTotalPrice(totalPrice);
			/* 应付金额,打折 */
			orders.setDealAmount(orders.getTotalPrice() * orders.getDiscount());
			/* 更新订单 */
			this.ordersDao.update(orders);
			
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersService(TOrders orders)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		orders.setUpdateTime(new Date());
		int res = this.ordersDao.update(orders);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), orders.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), orders);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TOrders> findCondListOrdersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<TOrders> apiResponse = new ApiResponse<TOrders>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<TOrders> demoOrdersList = Collections.EMPTY_LIST;

		Map<String, List<TOrders>> dataList = new HashMap<String, List<TOrders>>();
		demoOrdersList = this.ordersDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersList.iterator(); iterator.hasNext();)
			{
				TOrders orders = (TOrders) iterator.next();
				
				extMap.clear();
				extMap.put("id", orders.getUsersId());
				ApiResponse<TUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
				TUsers users = cateApiResponse.getDataOneJava();
				orders.setUsers(users);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TOrders> findOneOrdersService(Map<String, Object> condMap)
	{
		ApiResponse<TOrders> apiResponse = new ApiResponse<TOrders>();

		Map<String, TOrders> data = new HashMap<String, TOrders>();
		TOrders orders = this.ordersDao.findOne(condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", orders.getUsersId());
			ApiResponse<TUsers> cateApiResponse = this.usersDbService.findOneUsersService(extMap);
			TUsers users = cateApiResponse.getDataOneJava();
			orders.setUsers(users);
		}
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), orders);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(orders);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneOrdersGoodsService(TOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		ordersGoods.setCreateTime(new Date());
		ordersGoods.setUpdateTime(new Date());
		int res = this.ordersGoodsDao.save(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneOrdersGoodsService(TOrdersGoods ordersGoods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		ordersGoods.setUpdateTime(new Date());
		int res = this.ordersGoodsDao.update(ordersGoods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersGoods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.ordersGoodsDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TOrdersGoods> findCondListOrdersGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<TOrdersGoods> apiResponse = new ApiResponse<TOrdersGoods>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<TOrdersGoods> demoOrdersGoodsList = Collections.EMPTY_LIST;

		Map<String, List<TOrdersGoods>> dataList = new HashMap<String, List<TOrdersGoods>>();
		demoOrdersGoodsList = this.ordersGoodsDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoOrdersGoodsList.iterator(); iterator.hasNext();)
			{
				TOrdersGoods ordersGoods = (TOrdersGoods) iterator.next();
				
				extMap.clear();
				extMap.put("id", ordersGoods.getOrdersId());
				ApiResponse<TOrders> ordersApiResponse = this.findOneOrdersService(extMap);
				TOrders orders = ordersApiResponse.getDataOneJava();
				ordersGoods.setOrders(orders);
				
				extMap.clear();
				extMap.put("id", ordersGoods.getGoodsId());
				ApiResponse<AWares> goodsApiResponse = this.goodsDbService.findOneGoodsService(extMap);
				AWares goods = goodsApiResponse.getDataOneJava();
				ordersGoods.setGoods(goods);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoOrdersGoodsList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoOrdersGoodsList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TOrdersGoods> findOneOrdersGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<TOrdersGoods> apiResponse = new ApiResponse<TOrdersGoods>();

		Map<String, TOrdersGoods> data = new HashMap<String, TOrdersGoods>();
		TOrdersGoods ordersGoods = this.ordersGoodsDao.findOne(condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			extMap.clear();
			extMap.put("id", ordersGoods.getOrdersId());
			ApiResponse<TOrders> ordersApiResponse = this.findOneOrdersService(extMap);
			TOrders orders = ordersApiResponse.getDataOneJava();
			ordersGoods.setOrders(orders);
			
			extMap.clear();
			extMap.put("id", ordersGoods.getGoodsId());
			ApiResponse<AWares> goodsApiResponse = this.goodsDbService.findOneGoodsService(extMap);
			AWares goods = goodsApiResponse.getDataOneJava();
			ordersGoods.setGoods(goods);
		}
		data.put(ApiResponseEnum.NAME_ONE.getName(), ordersGoods);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(ordersGoods);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	public void setUsersDbService(IUsersDbService usersDbService)
	{
		this.usersDbService = usersDbService;
	}

	public void setGoodsDbService(IGoodsDbService goodsDbService)
	{
		this.goodsDbService = goodsDbService;
	}
}
